#!/usr/bin/env python  
# encoding: utf-8   

""" 
@version: v1.0 
@author: null 
@software: PyCharm 
@file: blogs.py 
@time: 2017.4.29 11:04 
"""

from ..auth import need_login
from weibo.models import Model
from weibo.url import URL
import weibo.settings as config
import urllib
from bs4 import BeautifulSoup
import re
import simplejson as json

class Blogs(Model):

    def __init__(self,isProxy=False):
        super(Blogs, self).__init__()
        self.logger.error('blogs')
        self.proxy = isProxy and config.proxy or {}
        pass

    #获取自己的微博
    @need_login
    def getMyselfBlogs(self,*args, **kwargs):

        return self.get(URL.myblogs(args[0]),proxy=self.proxy)
        pass

    #获取别人的微博
    @need_login
    def getHerselfBlogs(self,*args,**kwargs):

        return self.get(URL.herblogs(args[0],args[1]),proxy=self.proxy)
        pass

    #搜索
    @need_login
    def search(self,*args,**kwargs):

        searchPage = self.session.get(url=URL.searchPage(), headers=config.headers).text
        soup = BeautifulSoup(searchPage, 'html.parser')
        script = soup.body.find_all("script")
        jsonstr = re.findall("{.+}", script[0].string.split(";")[1])[0].replace("'", "\"")
        jsonobj = json.loads(jsonstr)
        luicode = jsonobj.get("common").get("params").get("luicode")
        featurecode = jsonobj.get("common").get("params").get("featurecode")
        oid = jsonobj.get("common").get("params").get("oid")

        #搜索关键字模拟
        # keydata={
        #     "q":str(args[0]),
        #     "count":"10"
        # }
        # searchKey = self.post(url=URL.searchKey(),header=config.searchKeyHeaders,data=keydata,proxy=self.proxy)
        # try:
        #     data = searchKey[0]
        #     print(data)
        # except:
        #     self.logger.error("请求搜索关键字错误")

        param = urllib.parse.quote(args[0])
        config.searchHeaders["Referer"]="http://m.weibo.cn/p/100103type%3D1%26q%3D"+param+"?type=all&queryVal="+param+"&luicode="+luicode+"&lfid=100103type%3D1%26q%3D"+param+"&title="+param
       # config.searchHeaders["Referer"]="http://m.weibo.cn/p/100103type%3D1%26q%3D%E8%80%81%E7%BD%97?type=all&queryVal=%E8%80%81%E7%BD%97&featurecode=20000320&oid=4102892316561530&luicode=10000011&lfid=106003type%3D1&title=%E8%80%81%E7%BD%97"
        return self.get(url=URL.search(param,featurecode,oid,luicode),header=config.searchHeaders,proxy=self.proxy)
        pass

    #发送评论
    @need_login
    def sendComment(self,*args,**kwargs):
        data={
            "content" : args[0],
            "id" : args[1],
            "st" : self.st
        }

        config.commentHeaders["Referer"]="http://m.weibo.cn/comment?id="+str(args[1])

        return self.post(url=URL.sendComment(),data=data,header=config.commentHeaders,proxy=self.proxy)

        pass

    #转发
    @need_login
    def rtBlog(self,*args,**kwargs):
        data = {
            "content": args[0],
            "id": args[1],
            "annotations":"",
            "st": self.st
        }

        config.commentHeaders["Referer"] = "http://m.weibo.cn/repost?id=" + str(args[1])

        return self.post(url=URL.rtBlog(), data=data, header=config.commentHeaders, proxy=self.proxy)

        pass